package br.com.reparoja.reparoja.view;

import android.content.Context;
import android.graphics.Outline;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;

/**
 * Created by Rogerio on 16/03/2015.
 */
public class RoundImageView extends ImageView {

    public RoundImageView(Context context) {
        super(context);
    }
    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr,
                                int defStyleRes) {
        super(context, attrs, defStyleAttr);

        setClickable(true);

        // Set the outline provider for this view. The provider is given the outline which it can
        // then modify as needed. In this case we set the outline to be an oval fitting the height
        // and width.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        outline.setOval(0, 0, getWidth(), getHeight());
                    }
                }
            });

            // Finally, enable clipping to the outline, using the provider we set above
            setClipToOutline(true);
        }
    }

}
